package com.dragon.modules.yuegou.service.impl.system;

import com.dragon.modules.yuegou.mapper.system.DailySeqMapper;
import com.dragon.modules.yuegou.mapper.system.SeqMapper;
import com.dragon.modules.yuegou.service.Sequence;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Component
public class SequenceImpl implements Sequence {

	public static final String INSTRUMENT_NO = "instrument_no";
	public static final String SKU_NO = "sku_no";
	public static final String ORDER_NO = "order_no";
	public static final String REFUND_NO = "refund_no";
	public static final String CARD_NO = "card_no";
	public static final String BUSD_ID = "busd_id";
	public static final String TWELVE = "twelve";
	public static final String ELEVEN = "eleven";
	public static final String THIRTEEN = "thirteen";
	public static final String FOURTEEN = "fourteen";
	public static final String FIFTEEN = "fifteen";
	public static final String SIXTEEN = "sixteen";

	private static final Set<String> DAILY_SEQ = new HashSet<>(Arrays.asList(ORDER_NO, REFUND_NO, CARD_NO));

	@Autowired
	private SeqMapper seqMapper;

	@Autowired
	private DailySeqMapper dailySeqMapper;

	/**
	 * 是否开发环境
	 */
	private boolean isDev;

	/**
	 * 是否QA环境
	 */
	private boolean isQa;

	/**
	 * 是否QA1环境
	 */
	private boolean isQa1;

	/**
	 * 是否生产环境
	 */
	private boolean isProd;

	/**
	 * 是否本地环境
	 */
	private boolean isLocal;

	@Autowired
	public void setEnv(Environment env) {
		List<String> profiles = Arrays.asList(env.getActiveProfiles());
		if (profiles.contains("prod")) {
			this.isProd = true;
		} else if (profiles.contains("qa")) {
			this.isQa = true;
		} else if (profiles.contains("qa1")) {
			this.isQa1 = true;
		} else if (profiles.contains("dev")) {
			this.isDev = true;
		}else {
			this.isLocal = true;
		}
	}

	/**
	 * 必须开启一个新事物来查询序列
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRES_NEW)
	public String next(String seqName) {
		return nextNoTransactional(seqName);
	}

	private String nextNoTransactional(String seqName){
		if (DAILY_SEQ.contains(seqName)) {
			return dailySeqMapper.nextId(seqName);
		} else {
			return seqMapper.nextId(seqName);
		}
	}

	/**
	 * 下一个sku编号
	 * 
	 * @return skuNo
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRES_NEW)
	public String nextSkuNo() {
		return nextNoTransactional(SKU_NO);
	}

	/**
	 * 下一个仪表编号编号
	 * 
	 * @return instrumentNo
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRES_NEW)
	public String nextInstrumentNo() {
		return nextNoTransactional(INSTRUMENT_NO);
	}

	/**
	 * 下一个订单号
	 * 
	 * @return orderNo
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRES_NEW)
	public String nextOrderNo() {
		if (isProd) {
			return nextNoTransactional(ORDER_NO);
		} else if (isQa) {
			return "100" + nextNoTransactional(ORDER_NO);
		} else if (isQa1) {
			return "101" + nextNoTransactional(ORDER_NO);
		}else if (isDev) {
			return "998" + nextNoTransactional(ORDER_NO);
		} else if (isLocal) {
			return "999" + nextNoTransactional(ORDER_NO);
		}
		return nextNoTransactional(ORDER_NO);
	}

	/**
	 * 下一个12
	 *
	 * @return orderNo
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRES_NEW)
	public String nextTwelve() {
		return "10"+nextNoTransactional(ELEVEN);
	}

	/**
	 * 下一个13
	 *
	 * @return orderNo
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRES_NEW)
	public String nextThirteen() {
		return "100"+nextNoTransactional(ELEVEN);
	}

	/**
	 * 下一个14
	 *
	 * @return orderNo
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRES_NEW)
	public String nextFourteen() {
		return "1000"+nextNoTransactional(ELEVEN);
	}

	/**
	 * 下一个15
	 *
	 * @return orderNo
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRES_NEW)
	public String nextFifteen() {
		return "10000"+nextNoTransactional(ELEVEN);
	}

	/**
	 * 下一个16
	 *
	 * @return orderNo
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRES_NEW)
	public String nextSixteen() {
		return "100000"+nextNoTransactional(ELEVEN);
	}

	/**
	 * 下一个退款单号
	 * 
	 * @return refundNo
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRES_NEW)
	public String nextRefundNo() {
		if (isProd) {
			return nextNoTransactional(REFUND_NO);
		} else if (isQa) {
			return "100" + nextNoTransactional(REFUND_NO);
		} else if (isQa1) {
			return "101" + nextNoTransactional(REFUND_NO);
		} else if (isDev) {
			return "998" + nextNoTransactional(REFUND_NO);
		} else if (isLocal) {
			return "999" + nextNoTransactional(REFUND_NO);
		}
		return nextNoTransactional(REFUND_NO);
	}

	/**
	 * 下一个悦卡号
	 * 
	 * @return cardNo
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRES_NEW)
	public String nextCardNo() {
		return nextNoTransactional(CARD_NO);
	}

	/**
	 * 下一个悦卡号
	 *
	 * @return cardNo
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRES_NEW)
	public String nextBusdId() {
		return nextNoTransactional(BUSD_ID);
	}

}
